package com.woyowodaows.application;

import android.annotation.TargetApi;
import android.app.Application;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.support.multidex.MultiDex;

import com.bumptech.glide.Glide;
import com.bumptech.glide.MemoryCategory;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.view.CropImageView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.tencent.bugly.beta.Beta;
import com.tencent.tinker.loader.app.DefaultApplicationLike;
import com.woyowodaows.R;
import com.woyowodaows.activity.MainActivity;
import com.woyowodaows.dao.DaoMaster;
import com.woyowodaows.dao.DaoSession;
import com.woyowodaows.utils.CrashHandlerUtils;
import com.woyowodaows.utils.FileUtils;
import com.woyowodaows.utils.GlideImageLoader;
import com.woyowodaows.utils.PicassoImageLoader;


import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;


public class MyApplication extends DefaultApplicationLike {

    private static Context mContext;
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    private long outTime = 30 * 1000;
    static DaoSession daoSession;

    public MyApplication(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplication().getApplicationContext();

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        //log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);

        //全局的读取超时时间
        builder.readTimeout(outTime, TimeUnit.MILLISECONDS);
        //全局的写入超时时间
        builder.writeTimeout(outTime, TimeUnit.MILLISECONDS);
        //全局的连接超时时间
        builder.connectTimeout(outTime, TimeUnit.MILLISECONDS);

        OkGo.getInstance().init((Application)mContext)
                .setOkHttpClient(builder.build())
                .setCacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .setRetryCount(1);


        //在这里为应用设置异常处理，然后程序才能获取未处理的异常
        CrashHandlerUtils.getInstance().init(mContext);

//        //初始化file文件
//        FileUtils.initAppFileFolder();

        Glide.get(mContext)
                .setMemoryCategory(MemoryCategory.HIGH);

        //初始化数据库
        initGreenDao();


        // 通过代码注册你的AppKey和AppSecret
//        MobSDK.init(mContext, "2863e91d33a00", "e81f06c0574e02015ed2125d2f8a684a");

//        initBugly();
        //设置拍照属性
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new PicassoImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        imagePicker.setCrop(true);                           //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);                   //是否按矩形区域保存
        imagePicker.setSelectLimit(1);              //选中数量限制
        imagePicker.setMultiMode(false);
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //
//
        //初始化极光推送
//        JPushInterface.setDebugMode(true); //正式环境时去掉此行代码
        JPushInterface.init(mContext);
//        jpushSet();
    }

    public static Context getApplicationContext() {
        return mContext;
    }


    //private void initBugly() {
//        // 设置是否开启热更新能力，默认为true
//        Beta.enableHotfix = true;
//        // 设置是否自动下载补丁，默认为true
//        Beta.canAutoDownloadPatch = true;
//        // 设置是否自动合成补丁，默认为true
//        Beta.canAutoPatch = true;
//        // 设置是否提示用户重启，默认为false
//        Beta.canNotifyUserRestart = false;
//        // 设置开发设备，默认为false，上传补丁如果下发范围指定为“开发设备”，需要调用此接口来标识开发设备
////        Bugly.setIsDevelopmentDevice(getApplication(), false);
//        // 多渠道需求塞入
//        // String channel = WalleChannelReader.getChannel(getApplication());
//        // Bugly.setAppChannel(getApplication(), channel);
//        // 这里实现SDK初始化，appId替换成你的在Bugly平台申请的appId
//        // 调试时，将第三个参数改为true
//        Bugly.init(getApplication(), "64827b063d", true);
//    }



//    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
//    @Override
//    public void onBaseContextAttached(Context base) {
//        super.onBaseContextAttached(base);
//        // you must install multiDex whatever tinker is installed!
//        MultiDex.install(base);
//        // 安装tinker
//        // TinkerManager.installTinker(this); 替换成下面Bugly提供的方法
//        Beta.installTinker(this);
//    }
//
//    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
//    public void registerActivityLifecycleCallback(
//            Application.ActivityLifecycleCallbacks callbacks) {
//        getApplication().registerActivityLifecycleCallbacks(callbacks);
//    }
//
    private void initGreenDao() {
        //创建数据库shop.db
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mContext, "woyowodaows.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取dao对象管理者
        daoSession = daoMaster.newSession();
    }


    public static DaoSession getDaoSession() {
        return daoSession;
    }

    private void jpushSet() {
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(mContext);
        builder.statusBarDrawable = R.mipmap.icon_logo;
        builder.notificationDefaults = Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE
                | Notification.DEFAULT_LIGHTS;  // 设置为铃声、震动、呼吸灯闪烁都要
        JPushInterface.setPushNotificationBuilder(1, builder);

        /**
         * 设置通知栏样式 - 定义通知栏Layout
         */

        CustomPushNotificationBuilder builder1 =
                new CustomPushNotificationBuilder(mContext, R.layout.customer_notitfication_layout,
                        R.id.icon, R.id.title, R.id.text);
        builder1.layoutIconDrawable = R.mipmap.icon_logo;
        builder.developerArg0 = "developerArg2";
        JPushInterface.setPushNotificationBuilder(2, builder1);
    }



}
